<template>
	<view>
		<customTabbar></customTabbar>
		<!-- 头像 -->
		<view class="kd_user_top" @tap="modalboxFn">
			<view class="kd_user_left_avatar">
				<view class="kd_user_avatar">
					<u-avatar :src="userInfo.avatar" size="140rpx"></u-avatar>
				</view>
				<view class="kd_user_text" v-show="!userNameIshow">点击登录</view>
				<view class="kd_user_text" v-show="userNameIshow">{{userInfo.username}}</view>
			</view>
			<!-- 工具栏部分 -->
			<view class="kd_user_right_arrow">
				<view v-show="!userNameIshow">
					<u-icon name="arrow-right" color="#fff" size="30rpx"></u-icon>
				</view>
				<view v-show="userNameIshow">
					<u-icon name="close" color="#fff" size="30rpx" @tap="quitFn"></u-icon>
				</view>
				
				
				</view>
		</view>
		<view class="kd_user_serve">
			<view class="kd_user_serve_item" v-for="item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]">
				<image class="kd_user_serve_img" src="../../static/logo.png" mode="widthFix"></image>
				<view class="kd_user_serve_text">我的订单</image></view>
			</view>
		</view>
		<!-- 退出模态框 -->
		<u-modal :show="show" :zoom="false" @confirm="confirm" @cancel="cancel" ref="uModal" :content="title" :asyncClose="true" showCancelButton></u-modal>
		<!-- 登录模态框 -->
		<view class="kd_user_modalBox" v-show="modalbox">
			<!-- 遮罩层 -->
			<view class="kd_user_box" @tap="modalbox=false"></view>
			<!-- 登录框 -->
			<view class="kd_user_login">
				<view class="kd_user_login_box kd_user_login_name">
					<text>用户名</text>
					<input v-model="username" type="text" placeholder="用户名" placeholder-style="font-size: 26rpx;">
				</view>
				<view class="kd_user_login_box kd_user_login_password">
					<text>密码</text>
					<input v-model="pwd" type="password" placeholder="密码" placeholder-style="font-size: 26rpx;">
				</view>
				<view class="kd_user_login_button" >
					<u-button @click="user_login" type="primary" text="登录/注册" color="rgb(252, 1, 5)" shape="circle"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import customTabbar from '@/custom-tab-bar/index.vue';
import {ApiGetUserData} from '@/utils/api.js'
import{mapMutations}from "vuex"
export default {
	components: {
		customTabbar
	},
	data() {
		return {
			title:'确定要退出登录吗？亲',
			modalbox:false,
			username:"",
			pwd:"",
			userInfo:{
				avatar:'https://cn.bing.com/images/search?view=detailV2&ccid=kHbxzgNM&id=88FEB7FADAD97FBCBE2A30441F5777954B1A5BF8&thid=OIP.kHbxzgNMA1OnpzCratDkigHaHa&mediaurl=https%3a%2f%2fts1.cn.mm.bing.net%2fth%2fid%2fR-C.9076f1ce034c0353a7a730ab6ad0e48a%3frik%3d%252bFsaS5V3Vx9EMA%26riu%3dhttp%253a%252f%252fpic.soutu123.com%252felement_origin_min_pic%252f01%252f55%252f09%252f5557474dac874a0.jpg!%252ffw%252f700%252fquality%252f90%252funsharp%252ftrue%252fcompress%252ftrue%26ehk%3dAWMYpfcbGCX17e6jZUBi%252bvRCi1Fj58Q0afl%252bZ%252bL3smE%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&exph=650&expw=650&q=%e5%a4%b4%e5%83%8f%e9%bb%98%e8%ae%a4%e5%9b%be%e7%89%87&simid=608048347510820114&FORM=IRPRST&ck=068C833D5EECCC543E5E9155DEE14868&selectedIndex=0&idpp=overlayview&ajaxhist=0&ajaxserp=0',
				username:"",
			},
			userNameIshow:false,
			show: false
		};
	},
	methods: {
		...mapMutations(['myToken']),
		async user_login(){
			if(!this.username){
				uni.showToast({
					title:"请输入用户名",
					icon:"error"
				})
				return
			}
			if(!this.pwd){
				uni.showToast({
					title:"请输入密码",
					icon:"error"
				})
				return
			}
			let res = await ApiGetUserData({
			username:this.username,
			pwd:this.pwd
			})
			console.log(res)
			if(res.errno!==0){
				console.log(111)
				uni.showToast({
					title:"请输入正确的账号密码",
					icon:"error"
				})
				return
			}
			this.modalbox=false
			this.username=''
			this.pwd=''
			uni.setStorageSync("token",res.data.token)
			uni.setStorageSync("userInfo",res.data.userInfo)
			let token = uni.getStorageSync("token")
			let userInfo = uni.getStorageSync("userInfo")
			this.myToken(token)
			// console.log(token)
			if(token){
				this.userInfo = userInfo
				this.userNameIshow = true
			}
		},
		modalboxFn(){
			let token = uni.getStorageSync("token")
			if(token) return
			this.modalbox=true
		},
		quitFn(){
			this.show = true;
		},
		confirm() {
			setTimeout(() => {
				// 0.5秒后自动关闭
				this.show = false;
			}, 500)
			uni.removeStorageSync("token")
			uni.removeStorageSync("userInfo")
			this.userNameIshow=false
		},
		cancel(){
			this.show = false;
		}
	},
	onShow() {
		this.$store.commit('tabbarIndex', 4);
		let userInfo = uni.getStorageSync("userInfo")
		this.userInfo = userInfo
		// console.log(userInfo)
		let token = uni.getStorageSync("token")
		if(token){
			this.userNameIshow=true
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #efefef;
}
.kd_user_top {
	height: 220rpx;
	background-color: #333333;
	display: flex;
	justify-content: space-between;
	align-items: center;
	.kd_user_left_avatar {
		.kd_user_avatar {
			margin: 0 20rpx;
		}
		display: flex;
		align-items: center;
		.kd_user_text {
			color: #fff;
			font-size: 28rpx;
		}
	}
	.kd_user_right_arrow {
		margin-right: 20rpx;
	}
}
.kd_user_serve {
	display: flex;
	gap: 2rpx;
	flex-wrap: wrap;

	.kd_user_serve_item {
		width: 249rpx;
		height: 249rpx;
		background-color: #fff;
		text-align: center;
		.kd_user_serve_img{
			width: 52rpx;
			margin-top: 70rpx;
		}
		.kd_user_serve_text{
			font-size: 22rpx;
			color: #6e7674;
			margin-top: 24rpx;
		}
		
	}
}

//登录模态框
.kd_user_modalBox{
	.kd_user_box{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
		background-color: rgba(0, 0, 0, 0.5);
	}
	.kd_user_login{
		width: 675rpx;
		height: 480rpx;
		background-color: #fff;
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		margin: auto;
		z-index: 2;
		.kd_user_login_box{
			width: 610rpx;
			height: 86rpx;
			border-bottom: 1rpx solid #ccc;
			margin: auto;
			display: flex;
			align-items: center;
			font-size: 26rpx;
			text{
				width: 198rpx;
			}
			imput{
				font-size: 26rpx;
			}
		}
		.kd_user_login_name{
			margin-top: 80rpx;
		}
		.kd_user_login_password{
			
		}
		.kd_user_login_button{
			margin: 0 auto;
			margin-top: 32rpx;
			width: 610rpx;
		}
	}
}

</style>
